<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class UserController extends Controller
{
    public function add(){
    	$title = '用户添加';
    	return view('admin.user.add',['title'=>$title]);
    }

    public function insert(Request $request){
    	$data = $request -> except(['_token']);
    	//判断用户名是否为空
    	//laravel内置验证
    	$this ->validate($request,[
    		'username'=>'required',
    		'password'=>'required',
    		'repassword'=>'required|same:password',
    		'email'=>'required|email',
    		'phone'=>'required'
    		],[
    		'username.required'=>'用户名不能为空',
    		'password.required'=>'密码不能为空',
    		'repassword.required'=>'确认密码不能为空',
    		'repassword.same'=>'确认密码不一样',
    		'email.required'=>'邮箱不能为空',
    		'email.email'=>'邮箱格式不能正确',
    		'phone.required'=>'手机号码不能为空']);
    	//处理pic字段
    	if($request ->hasfile('pic'))
    	{
    		if($request ->file('pic')->isValid())
				{
					$suffix = $request->file('pic')->getClientOriginalExtension();
						//生成随机文件名称
						$filename = time().mt_rand(10000,99999).'.'.$suffix;
						//yidoing文件
						$request -> file('pic')->move('./uploads',$filename);
						$data['pic'];
						//拼接pic路径
						$data['pic'] = '/uploads/'.$filename;
				}
			}
			//处理密码字段
			$data['password']=Hash::make($data['password']);

			//添加status字段
			$data['status']=1;
			$data['token']=str_random(50);
			//删除repassword字段
			unset($data['repassword']);
			
			//执行添加操作
			$res = DB::table('user')->insert($data);
			//
			if($res)
			{
				return redirect('/admin/user/index');
			}else{
				return back();
			}  	

    }

    //index
    public function index(Request $request)
    {
        
    	//查询数据
    	$data =  DB::table('user')
         ->where('username','like','%'.$request->input('keywords').'%')
         ->paginate($request->input('num',1));
         // ->paginate(5);

    	$title = '用户列表';
    	return view('admin.user.index',['title'=>$title,'data'=>$data,'request'=>$request->all()]);    }

    public function edit($id)
    {

    	//查询数据
    	$data =DB::table('user') -> where('id',$id)->first(); 
    	$title = '用户编辑';
    	return view('admin.user.edit',['title'=>$title,'data'=>$data]);
    }
    

    	 public function update(Request $request){
    	 	$data = $request -> except(['_token','id','oldpic']);
    	 	//laravel内置验证
    	 	$id = $request ->input('id');

    	$this ->validate($request,[
    		'username'=>'required',
    		
    		'email'=>'required|email',
    		'phone'=>'required'
    		],[
    		'username.required'=>'用户名不能为空',
    		
    		'email.required'=>'邮箱不能为空',
    		'email.email'=>'邮箱格式不能正确',
    		'phone.required'=>'手机号码不能为空']);
    	//处理pic字段
    	if($request ->hasfile('pic'))
    	{
    		if($request ->file('pic')->isValid())
				{
					$suffix = $request->file('pic')->getClientOriginalExtension();//获取图片格式
						//生成随机文件名称
						$filename = time().mt_rand(10000,99999).'.'.$suffix;
						//yidoing文件
						$request -> file('pic')->move('./uploads',$filename);
						$data['pic'];
						//拼接pic路径
						$data['pic'] = '/uploads/'.$filename;
						//删除老图
						//

						$oldpic = '.'.$request -> input('oldpic');
						
						if(file_exists($oldpic))
						{
							unlink($oldpic);
						}
				}
			}
			//执行添加操作
			$res = DB::table('user')->where('id',$id)->update($data);
			//
			if($res)
			{
				return redirect('/admin/user/index')->with(['info'=>'更新成功']);
			}else{
				return back();
			}  
    	 }
    	 public function delete($id){
    	 	$res= DB::table('user') ->where('id',$id)->delete();
    	 	if($res)
			{
				return redirect('/admin/user/index')->with(['info'=>'删除成功']);
			}else{
				return redirect('/admin/user/index')->with(['info'=>'删除不成功']);
			} 
    	 }
}
